/**
 * 2006-12-8 10:55:37
 * FtpUtil.java
 */
package org.lilystudio.veryftp.ftp;

import java.io.IOException;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

/**
 * @author Samson
 * @version 0.1
 */
public class FtpUtil extends FTPClient {

	public boolean connectAndLogin(String host, int port, String userName,
			String password) throws Exception {
		boolean success = false;
		connect(host, port);
		int reply = getReplyCode();
		if (FTPReply.isPositiveCompletion(reply))
			success = login(userName, password);
		if (!success)
			disconnect();
		return success;
	}

	public void logoutAndDisconncect() {
		try {
			logout();
			disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setPassiveMode(boolean setPassive) {
		if (setPassive)
			enterLocalPassiveMode();
		else
			enterLocalActiveMode();
	}

	public boolean useAsciiType() throws IOException {
		return setFileType(FTP.ASCII_FILE_TYPE);
	}

	public boolean useBinaryType() throws IOException {
		return setFileType(FTP.BINARY_FILE_TYPE);
	}

}
